package com.yilin.tms.gps.service.impl;

import com.google.gson.reflect.TypeToken;
import com.yilin.commons.exception.BusinessException;
import com.yilin.commons.util.*;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.gps.GpsPoint;
import com.yilin.tms.core.entity.gps.gaode.GeoCalResult;
import com.yilin.tms.core.entity.gps.gaode.GeoRouteResult;
import com.yilin.tms.core.entity.gps.gaode.geo_track.GeoLocation;
import com.yilin.tms.core.entity.gps.gaode.geo_track.GeoPointInfo;
import com.yilin.tms.core.entity.gps.gaode.geo_track.GeoPointTrack;
import com.yilin.tms.core.entity.gps.gaode.geo_track.GeoTrackResult;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrderNote;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.hatch.HatchGaodeMap;
import com.yilin.tms.core.entity.user.hatch.HatchPlatProduct;
import com.yilin.tms.gps.service.IGaodeWebService;
import com.yilin.tms.core.remote.IUserRemote;
import com.yilin.tms.gps.service.ITrailStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 高德地图Service
 *
 * @author zhangjinyuan
 * @date 2022/9/16
 */
@Service
@Transactional
public class GaodeWebService implements IGaodeWebService {
    // 用于存放孵化配置，不反复请求数据
    private final Map<String,HatchGaodeMap> hatchGaodeData = new HashMap<>();

    //线路规划
    private static final String drivingURL = "https://restapi.amap.com/v5/direction/driving";
    //地理编码
    private static final String geoUrl = "https://restapi.amap.com/v3/geocode/geo";
    //逆地理编码
    private static final String reGeoUrl = "https://restapi.amap.com/v3/geocode/regeo";
    //添加猎鹰服务
    private static final String serviceAddUrl = "https://tsapi.amap.com/v1/track/service/add";
    //获取猎鹰服务
    private static final String serviceListUrl = "https://tsapi.amap.com/v1/track/service/list";
    //删除猎鹰服务
    private static final String serviceDeleteUrl = "https://tsapi.amap.com/v1/track/service/delete";
    //添加猎鹰终端
    private static final String terminalAddUrl = "https://tsapi.amap.com/v1/track/terminal/add";
    //终端搜索
    private static final String terminalListUrl =  "https://tsapi.amap.com/v1/track/terminal/list";
    //删除猎鹰终端
    private static final String terminalDeleteUrl = "https://tsapi.amap.com/v1/track/terminal/delete";
    //添加猎鹰轨迹
    private static final String traceAddUrl = "https://tsapi.amap.com/v1/track/trace/add";
    //删除猎鹰轨迹
    private static final String traceDeleteUrl = "https://tsapi.amap.com/v1/track/trace/delete";
    //上传轨迹点
    private static final String pointUploadUrl = "https://tsapi.amap.com/v1/track/point/upload";
    //猎鹰轨迹位置
    private static final String traceLastpointUrl = "https://tsapi.amap.com/v1/track/terminal/lastpoint";
    //猎鹰轨迹
    private static final String traceTrsearchUrl = "https://tsapi.amap.com/v1/track/terminal/trsearch";
    //猎鹰轨迹行为
    private static final String traceDrivingbehaviorUrl = "https://tsapi.amap.com/v1/track/analysis/drivingbehavior";
    //猎鹰轨迹停留
    private static final String traceStaypointUrl = "https://tsapi.amap.com/v1/track/analysis/staypoint";


    @Resource
    IUserRemote userRemote;
    @Resource
    ITrailStorageService trailStorageService;

    /**
     * 获取孵化配置：注意，只获取一次，重新设置后必须重启服务
     * */
    @Override
    public HatchGaodeMap getHatchGaode(String platId) {
        if (hatchGaodeData.containsKey(platId)) return hatchGaodeData.get(platId);
        HatchGaodeMap gaodeMap = ParamUtil.isSuccess(userRemote.getGaodeMapHatch(platId));
        if (gaodeMap == null) throw new BusinessException("平台未初始化高德地图API配置!");
        if (StringUtil.isEmpty(gaodeMap.getGdWebKey()))  throw new BusinessException("平台未设置高德地图API配置的Key!");
        hatchGaodeData.put(platId,gaodeMap);
        return gaodeMap;
    }

    @Override
    public HatchPlatProduct getHatchPlatProduct(String platId) {
        return ParamUtil.isSuccess(userRemote.getPlatProductHatch(platId));
    }

    /**
     * 高德地图 分别规划距离和时间
     */
    @Override
    public GeoCalResult getDrivingCalculation(String platId, String startCoordinates, String endCoordinates) {
        List<String> startCoordinateList = new ArrayList<>(Arrays.asList(startCoordinates.split(";")));
        List<String> endCoordinateList = new ArrayList<>(Arrays.asList(endCoordinates.split(";")));
        //TODO: 起点排序 使用高德轨迹接口并发量太大，暂时使用直线计算, 后期可直接在接口内更改 p:最近点 ,a,b:排序点
        List<String> startSortCoordinateList = ArrayUtil.sortPList(startCoordinateList.get(0), new ArrayList<>(startCoordinateList), new ArrayList<>(), (p, a, b) -> {
            double v = GpsMapUtil.getDistance(p, a) - GpsMapUtil.getDistance(p, b);
            if (v > 0) return 1;
            if (v < 0) return -1;
            return 0;
        });
        List<String> endSortCoordinateList = ArrayUtil.sortPList(ArrayUtil.last(startSortCoordinateList), new ArrayList<>(endCoordinateList), new ArrayList<>(), (p, a, b) -> {
            double v = GpsMapUtil.getDistance(p, a) - GpsMapUtil.getDistance(p, b);
            if (v < 0) return -1;
            if (v > 0) return 1;
            return 0;
        });
        String startCoordinate = ArrayUtil.first(startSortCoordinateList);
        String endCoordinate = ArrayUtil.last(endSortCoordinateList);

        List<String> waypoints = new ArrayList<>();
        waypoints.add(startCoordinate);
        for (String coordinate : startSortCoordinateList) {
            if (!coordinate.equals(startCoordinate)) ArrayUtil.notRepeatAdd(waypoints, coordinate);
        }
        for (String coordinate : endSortCoordinateList) {
            if (!coordinate.equals(endCoordinate)) ArrayUtil.notRepeatAdd(waypoints, coordinate);
        }
        waypoints.add(endCoordinate);
        //每页17个点，
        return nextDrivingCalculation(new GeoCalResult(),platId, waypoints,1,17);
    }

    /**
     * 高德地图 根据地址获取坐标位置 地理编码
     */
    @Override
    public String getPointByAddress(String platId, String address) {
        Map<String, Object> map = new HashMap<>();
        map.put("address", address);
        map.put("key", getHatchGaode(platId).getGdWebKey());
        Map<String, Object> returnMap = get(geoUrl, map);
        List<Map<String, Object>> result = (List<Map<String, Object>>) returnMap.get("geocodes");
        if (result != null) return (String) result.get(0).get("location");
        return null;
    }

    /**
     * 高德地图 根据地址获取坐标位置 地理编码
     */
    @Override
    public String getAdCodeByAddress(String platId, String address) {
        Map<String, Object> map = new HashMap<>();
        map.put("address", address);
        map.put("key", getHatchGaode(platId).getGdWebKey());
        Map<String, Object> returnMap = get(geoUrl, map);
        List<Map<String, Object>> result = (List<Map<String, Object>>) returnMap.get("geocodes");
        if (result != null) return (String) result.get(0).get("adcode");
        return null;
    }

    /**
     * 高德地图 根据坐标位置获取地址 逆地理编码
     */
    @Override
    public String getAddressByPoint(String platId, String point) {
        Map<String, Object> map = new HashMap<>();
        map.put("location", point);
        map.put("key", getHatchGaode(platId).getGdWebKey());
        Map<String, Object> returnMap = get(reGeoUrl, map);
        Map<String, Object> result = (Map<String, Object>) returnMap.get("regeocode");
        if (result != null) return (String) result.get("formatted_address");
        return null;
    }

    /**
     * 高德地图 根据坐标位置获取地址及地区编码 逆地理编码
     */
    @Override
    public Map<String, Object> getAddressInfoByPoint(String platId, String point) {
        Map<String, Object> map = new HashMap<>();
        map.put("location", point);
        map.put("key", getHatchGaode(platId).getGdWebKey());
        Map<String, Object> returnMap = get(reGeoUrl, map);
        Map<String, Object> result = (Map<String, Object>) returnMap.get("regeocode");
        if (result==null) return null;
        Map<String, Object> addressComponent = ( Map<String, Object> ) result.get("addressComponent");
        if (addressComponent==null) return null;
        String adcode = (String) addressComponent.get("adcode");
        String address =  (String) result.get("formatted_address");
        return MapUtil.generate(map1 -> {
            map1.put("adcode",adcode);
            map1.put("address",address);
        });
    }

    /**
     * 新 猎鹰轨迹 增加服务
     */
    @Override
    public String addTrackService(String gdWebKey, String platCode, String platName) {
        List<Map<String, Object>> serviceList = getTrackServiceList(gdWebKey);
        Map<String, Object> containsBy = ArrayUtil.getBy(serviceList, obj -> obj.get("name").equals(platCode));
        if (containsBy != null) return String.valueOf(containsBy.get("sid"));
        Map<String, String> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("name", platCode);
        map.put("desc", platName);
        Map<String, Object> returnMap = post(serviceAddUrl, map);
        Map<String, Object> data = (Map<String, Object>) returnMap.get("data");
        if (data == null) return null;
        return String.valueOf( data.get("sid"));
    }

    /**
     * 新 猎鹰轨迹 删除服务
     */
    @Override
    public void deleteTrackService(String platId, String sid) {
        Map<String, String> map = new HashMap<>();
        map.put("key", getHatchGaode(platId).getGdWebKey());
        map.put("sid", sid);
        post(serviceDeleteUrl, map);
    }

    /**
     * 新 猎鹰轨迹 查詢服务
     */
    @Override
    public List<Map<String, Object>> getTrackServiceList(String gdWebKey) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", gdWebKey);
        Map<String, Object> returnMap = get(serviceListUrl, map);
        Map<String, Object> data = (Map<String, Object>) returnMap.get("data");
        if (data == null) return new ArrayList<>();
        List<Map<String, Object>> results = (List<Map<String, Object>>) data.get("results");
        if (results == null) return new ArrayList<>();
        return results;
    }

    /**
     * 新 猎鹰轨迹 添加终端
     */
    @Override
    public String addTrackTerminal(String gdWebKey, String geoTrackSid, String userId, String desc) {
        List<Map<String, Object>> serviceList = getTrackTerminalList(gdWebKey,geoTrackSid,userId);
        Map<String, Object> containsBy = ArrayUtil.getBy(serviceList, obj -> obj.get("name").equals(userId));
        if (containsBy != null) return String.valueOf(containsBy.get("tid"));
        Map<String, String> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("sid", geoTrackSid);
        map.put("name", userId);//司机ID等
        map.put("desc", desc);
        Map<String, Object> returnMap = post(terminalAddUrl, map);
        Map<String, Object> data = (Map<String, Object>) returnMap.get("data");
        if (data == null) return null;//20009
        return String.valueOf(data.get("tid"));
    }

    /**
     * 新 猎鹰轨迹 添加终端
     */
    @Override
    public List<Map<String, Object>> getTrackTerminalList(String gdWebKey, String geoTrackSid, String terminalName) {
        Map<String, String> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("sid", geoTrackSid);
        map.put("name", terminalName);//司机ID等
        Map<String, Object> returnMap = post(terminalListUrl, map);
        Map<String, Object> data = (Map<String, Object>) returnMap.get("data");
        if (data == null) return new ArrayList<>();
        List<Map<String, Object>> results = (List<Map<String, Object>>) data.get("results");
        if (results == null) return new ArrayList<>();
        return results;
    }

    /**
     * 新 猎鹰轨迹 删除终端
     */
    @Override
    public void deleteTrackTerminal(String platId, String sid, String tid) {
        Map<String, String> map = new HashMap<>();
        map.put("key", getHatchGaode(platId).getGdWebKey());
        map.put("sid", sid);
        map.put("tid", tid);
        post(terminalDeleteUrl, map);
    }

    /**
     * 新 猎鹰轨迹 添加轨迹
     * 已过时 由前台创建
     */
    @Override
    public String addTrackTrace(String gdWebKey, String sid,String tid, String transportNo) {
        Map<String, String> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("sid", sid);
        map.put("tid", tid);
        map.put("trname", transportNo);//运单号
        Map<String, Object> returnMap = post(traceAddUrl, map);
        Map<String, Object> data = (Map<String, Object>) returnMap.get("data");
        if (data == null) return null;
        return String.valueOf(data.get("trid")) ;
    }

    /**
     * 新 猎鹰轨迹 删除轨迹
     */
    @Override
    public String deleteTrackTrace(String platId, String sid, String tid, String traceId) {
        Map<String, String> map = new HashMap<>();
        map.put("key", getHatchGaode(platId).getGdWebKey());
        map.put("sid", sid);
        map.put("tid", tid);
        map.put("trid", traceId);//运单号
        post(traceDeleteUrl, map);
        return "SUCCESS";
    }

    /**
     * 新 猎鹰轨迹 上传轨迹点
     */
    @Override
    public void addTracePoints(String platId, String sid, String tid, String traceId, List<GeoLocation> geoLocationList) {
        int totalPage = geoLocationList.size() % 100;
        for (int i = 0; i < totalPage; i++) {
            String points = JsonUtil.objectToJson(ArrayUtil.paging(geoLocationList,i+1,100));
            addPoints( platId,sid,tid,traceId, points);
        }
    }

    private void addPoints(String platId, String sid, String tid, String traceId,String points) {
        Map<String, String> map = new HashMap<>();
        map.put("key", getHatchGaode(platId).getGdWebKey());
        map.put("sid", sid);
        map.put("tid", tid);
        map.put("trid", traceId);//
        map.put("points", points);//
        Map<String, Object> post = post(pointUploadUrl, map);
        System.out.println(post.toString());
    }

    /**
     * 新 猎鹰轨迹 查询位置
     */
    @Override
    public GeoLocation queryTraceLastPoint(String gdWebKey, String serviceId, String terminalId, String traceId) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("sid", Long.parseLong(serviceId));
        map.put("tid", Long.parseLong(terminalId));
        map.put("trid", Long.parseLong(traceId));
        map.put("correction", "n");
        String returnData = HttpService.doGet(traceLastpointUrl, map);
        GeoTrackResult<GeoLocation> result = JsonUtil.jsonToObject(returnData,new TypeToken<GeoTrackResult<GeoLocation>>(){});
        if (result.getData() == null) return null;
        return result.getData();
    }

    /**
     * 新 猎鹰轨迹 查询轨迹 4b439720c7630d4fcd67beafab3ea4b1 895038 648957387 20
     * correction: 显示轨迹时传true，备份轨迹传false
     */
    @Override
    public GeoPointTrack queryTrackTrace(String gdWebKey,Boolean correction, String serviceId, String terminalId, String traceId, Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("sid", serviceId);
        map.put("tid", terminalId);
        map.put("trid", traceId);
        if (correction){  //返回原始信息时不传下面三个
            map.put("correction", "denoise=1,mapmatch=1,attribute=1,threshold=20,mode=driving");
            map.put("recoup", "1");
            map.put("gap", "3000");
        }
        map.put("ispoints", "1");
        map.put("page", page);
        map.put("pagesize", limit);
        String returnData = HttpService.doGet(traceTrsearchUrl, map);
        GeoTrackResult<GeoPointInfo> result = JsonUtil.jsonToObject(returnData,new TypeToken<GeoTrackResult<GeoPointInfo>>(){});
        if (result.getData() == null) return null;
        List<GeoPointTrack> trackList = result.getData().getTracks();
        if (trackList == null || trackList.size()==0) return null;
        return trackList.get(0);
    }

    /**
     *驾驶行为
     */
    @Override
    public List<Map<String, Object>> queryDrivingBehavior(String gdWebKey, String serviceId, String terminalId, String traceId) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("sid", serviceId);
        map.put("tid", terminalId);
        map.put("trid", traceId);
        String returnData = HttpService.doGet(traceDrivingbehaviorUrl, map);
        Map<String, Object> returnMap = JsonUtil.jsonToObject(returnData, new TypeToken<Map<String, Object>>() {
        });
        return (List<Map<String, Object>>) returnMap.get("data");
    }

    /**
     * 停车点 20米10分钟不移动
     * */
    @Override
    public List<Map<String, Object>> queryStayPoint(String gdWebKey, String serviceId, String terminalId, String traceId) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", gdWebKey);
        map.put("sid", serviceId);
        map.put("tid", terminalId);
        map.put("trid", traceId);
        String returnData = HttpService.doGet(traceStaypointUrl, map);
        Map<String, Object> returnMap = JsonUtil.jsonToObject(returnData, new TypeToken<Map<String, Object>>() {
        });
        return (List<Map<String, Object>>) returnMap.get("data");
    }

    /**
     * 高德 分页规划
     * page 起点是 1 limit不要超过17
     */
    private GeoCalResult nextDrivingCalculation(GeoCalResult allResult,String platId,List<String> wayPoints,int page,int limit){
        List<String> pagePoints = ArrayUtil.paging(wayPoints, page, limit);
        if(pagePoints.size() == 0) return allResult;
        if(page > 1) pagePoints.add(0, ArrayUtil.pagingLast(wayPoints, page-1, limit));//加入上一页的最后一个点，插在首位
        GeoCalResult geoCalResult = drivingCalculation(platId, pagePoints);
        allResult.setDuration( allResult.getDuration() + geoCalResult.getDuration());
        allResult.setDistance( allResult.getDistance() + geoCalResult.getDistance());
        return nextDrivingCalculation(allResult,platId,wayPoints,page+1,limit);
    }

    private GeoCalResult drivingCalculation(String platId, List<String> waypoints) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("origin", ArrayUtil.first(waypoints));
            map.put("destination", ArrayUtil.last(waypoints));
            map.put("key", getHatchGaode(platId).getGdWebKey());
            map.put("sig", getHatchGaode(platId).getGdSig());
            map.put("strategy", "0");
            map.put("output", "JSON");
            map.put("waypoints", waypoints.subList(1,waypoints.size()-1));
            map.put("show_fields", "cost,tmcs");
            String returnData = HttpService.doGet(drivingURL, map);
            GeoRouteResult result = JsonUtil.jsonToObject(returnData, new TypeToken<GeoRouteResult>() {});
            if (!result.getStatus().equals("1")) throw new BusinessException(result.getInfo());
            if (result.getRoute() == null) throw new BusinessException("线路规划未返回结果");
            if (result.getRoute().getPaths().size() == 0) throw new BusinessException("线路规划未返回结果");
            GeoRouteResult.Route.Path path = result.getRoute().getPaths().get(0);
            GeoCalResult calResult = new GeoCalResult();
            calResult.setDistance(Double.parseDouble(path.getDistance()));
            calResult.setDuration(Double.parseDouble(path.getCost().getDuration()));
            return calResult;
        } catch (Exception e) {
            throw new BusinessException("高德地图计算路程时间报错，错误信息：" + e.getMessage());
        }
    }
    /**
     * 高德算路 规划
     */
    private GeoRouteResult drivingCalculation(String platId, String startCoordinate,String endCoordinate) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("origin", startCoordinate);
            map.put("destination", endCoordinate);
            map.put("key", getHatchGaode(platId).getGdWebKey());
            map.put("sig", getHatchGaode(platId).getGdSig());
            map.put("strategy", "0");
            map.put("output", "JSON");
            //map.put("waypoints", waypoints);
            map.put("show_fields", "cost,tmcs");
            String returnData = HttpService.doGet(drivingURL, map);
            GeoRouteResult result = JsonUtil.jsonToObject(returnData, new TypeToken<GeoRouteResult>() {
            });
            if (!result.getStatus().equals("1")) throw new BusinessException(result.getInfo());
            if (result.getRoute() == null) throw new BusinessException("线路规划未返回结果");
            if (result.getRoute().getPaths().size() == 0) throw new BusinessException("线路规划未返回结果");
            return result;
        } catch (Exception e) {
            throw new BusinessException("高德地图计算路程时间报错，错误信息：" + e.getMessage());
        }
    }
    /**
     * 生成运单轨迹
     */
    @Override
    public void generateOrderTrajectory(Account loginUser, TransitOrder transitOrder) {
        //根据运单点排序
        Map<String, Object> map = sortOrderCoordinate(transitOrder);
        //获取高德路线规划
        GeoRouteResult geoRouteResult = drivingCalculation(loginUser.getOrganizeId(),map.get("startCoordinate").toString(),map.get("endCoordinate").toString());
        //根据规划路线生成轨迹
        List<GpsPoint> pointList = createGpsPointByPlanRoute(transitOrder, geoRouteResult);
        //保存轨迹
        trailStorageService.addTrackPoints(pointList);
    }
    /**
     * 根据运单点排序
     */
    private Map<String, Object> sortOrderCoordinate(TransitOrder transitOrder) {
        Map<String, Object> map = new HashMap<>();
        String startCoordinate = "";
        List<String> waypoints = new ArrayList<>();
        String endCoordinate = "";
        List<TransitOrderNote> orderDetailNoteList = transitOrder.getTransitOrderNoteList();
        if (transitOrder.getGoodsOrderType() == GoodsOrder.GoodsOrderType.warehouseCollect) {
            for (int i = 0; i < orderDetailNoteList.size(); i++) {
                TransitOrderNote orderDetailNote = orderDetailNoteList.get(i);
                if (i == 0) {
                    startCoordinate = orderDetailNote.getConsignorCoordinate();
                } else {
                    waypoints.add(orderDetailNote.getConsignorCoordinate());
                }
                if (i == (orderDetailNoteList.size() - 1)) {
                    endCoordinate = orderDetailNote.getConsigneeCoordinate();
                }
            }
        } else {
            for (int i = 0; i < orderDetailNoteList.size(); i++) {
                TransitOrderNote orderDetailNote = orderDetailNoteList.get(i);
                if (i == (orderDetailNoteList.size() - 1)) {
                    endCoordinate = orderDetailNote.getConsigneeCoordinate();
                } else {
                    waypoints.add(orderDetailNote.getConsigneeCoordinate());
                }
                if (i == 0) {
                    startCoordinate = orderDetailNote.getConsignorCoordinate();
                }
            }
        }
        map.put("startCoordinate", startCoordinate);
        map.put("waypoints", waypoints);
        map.put("endCoordinate", endCoordinate);
        return map;
    }
    /**
     * 内部 根据规划路线生成轨迹
     */
    private List<GpsPoint> createGpsPointByPlanRoute(TransitOrder transitOrder, GeoRouteResult geoRouteResult) {
        List<GpsPoint> pointList = new ArrayList<>();
        String traceId = transitOrder.getTransportNo();
        Date startTransitTime = transitOrder.getStartTransitTime();
        List<GeoRouteResult.Route.Path.Step> steps = geoRouteResult.getRoute().getPaths().get(0).getSteps();
        for (GeoRouteResult.Route.Path.Step step : steps) {
            GeoRouteResult.Route.Path.Cost cost = step.getCost();
            String duration = cost.getDuration();
            List<GeoRouteResult.Route.Path.Tmc> tmcs = step.getTmcs();
            int tmcAverageDuration = Integer.parseInt(duration) / tmcs.size();
            int speed = (Integer.parseInt(step.getStep_distance()) / Integer.parseInt(duration)) * 60 * 60 / 1000;
            for (GeoRouteResult.Route.Path.Tmc tmc : tmcs) {
                String tmcPolyline = tmc.getTmc_polyline();
                String[] points = tmcPolyline.split(";");
                for (String point : points) {
                    GpsPoint gpsPoint = new GpsPoint();
                    //经纬度
                    String[] gdLntLat = point.split(",");
                    gpsPoint.setGdLntLat(new double[]{Double.parseDouble(gdLntLat[0]), Double.parseDouble(gdLntLat[1])});
                    double[] bdLngLat = GpsMapUtil.bdEncrypt(gpsPoint.getGdLntLat()[0], gpsPoint.getGdLntLat()[1]);
                    gpsPoint.setBdLngLat(new double[]{bdLngLat[1], bdLngLat[0]});
                    //方向
                    gpsPoint.setBear(getGDRouteDirection(step.getOrientation()));
                    //时间
                    int averageDuration = tmcAverageDuration / points.length;
                    startTransitTime = DateTimeUtil.plusDate(startTransitTime, averageDuration, Calendar.SECOND);
                    gpsPoint.setGpsTime(startTransitTime);
                    //速度
                    gpsPoint.setSpeed((double) speed);
                    gpsPoint.setTraceId(traceId);
                    pointList.add(gpsPoint);
                }
            }
        }
        return pointList;
    }
    /**
     * 获取高德地图路线规划的方向
     *
     * @return 方向度数
     */
    private Double getGDRouteDirection(String orientation) {
        switch (orientation) {
            case "东":
                return 90D;
            case "东北":
                return 45D;
            case "东南":
                return 135D;
            case "西":
                return 270D;
            case "西北":
                return 315D;
            case "西南":
                return 225D;
            case "南":
                return 180D;
            default:
                return 0D;
        }
    }
    private Map<String, Object> post(String url, Map<String, String> map) {
        String returnData = HttpService.doPost(url, map);
        return JsonUtil.jsonToObject(returnData, new TypeToken<Map<String, Object>>() {
        });
    }

    private Map<String, Object> get(String url, Map<String, Object> map) {
        String returnData = HttpService.doGet(url, map);
        return JsonUtil.jsonToObject(returnData, new TypeToken<Map<String, Object>>() {
        });
    }
}
